package java8_NewFeature.StreamApi;

import org.junit.jupiter.api.Test;

import java.util.Arrays;
import java.util.List;
import java.util.Optional;
import java.util.stream.Collectors;
import java.util.stream.Stream;

public class StreamApi3 {
    //归约
@Test
    void test1(){
//    reduce(T iden,BinaryOperator b):可以将流中元素反复结合起来，得到一个值。返回T

//    练习1：计算1-10的和
    List<Integer> integers = Arrays.asList(1, 2, 3, 4, 5, 6, 7, 8, 9, 10);
    Integer sum = integers.stream().reduce(0, Integer::sum);
    System.out.println(sum);


//    reduce(BinaryOperator)--可以将流中元素反复结合起来，得到一个Optional<T>
//    练习2.计算所有员工的薪水
    List<Employee> employees = EmployeeData.getEmployees();
    Stream<Double> doubleStream = employees.stream().map(e -> e.sal);
    Optional<Double> reduce = doubleStream.reduce(Double::sum);
    System.out.println(reduce);

}

// 4.收集
    @Test
    void test01(){
//    collect(Collector)---将流转换会其他形式。接受一个Collector接口的实现，用于给Stream中元素做汇总的方法
//     练习1：查找工资大于6000的员工，结果犯规为一个List或Set
        List<Employee> employees = EmployeeData.getEmployees();
        Stream<Employee> employeeStream = employees.stream().filter(e -> e.sal > 7000);
        List<Employee> collect = employeeStream.collect(Collectors.toList());
        System.out.println(collect);
    }


}
